//
//  AppInfo+Path.m
//  Project
//
//  Created by Suny on 14-8-23.
//  Copyright (c) 2014年 Suny. All rights reserved.
//

#import "AppInfo+Path.h"

@implementation AppInfo (Path)

+ (NSString *)appPath
{
	return [[AppInfo sharedInstance] appPath];
}

- (NSString *)appPath
{
	
	NSError * error = nil;
	NSArray * paths = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:NSHomeDirectory() error:&error];
	
    NSString *appPath = nil;
    for ( NSString * path in paths )
	{
		if ( [path hasSuffix:@".app"] )
		{
			appPath = [NSString stringWithFormat:@"%@/%@", NSHomeDirectory(), path];
			break;
		}
	}
	
	return appPath;
}

+ (NSString *)docPath
{
	return [[AppInfo sharedInstance] docPath];
}

- (NSString *)docPath
{
	
	NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = [paths objectAtIndex:0];
	
	return docPath;
}

+ (NSString *)libPrefPath
{
	return [[AppInfo sharedInstance] libPrefPath];
}

- (NSString *)libPrefPath
{
	NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
	NSString * path = [[paths objectAtIndex:0] stringByAppendingFormat:@"/Preference"];
	
	[self touch:path];
	
	
	return path;
}

+ (NSString *)libCachePath
{
	return [[AppInfo sharedInstance] libCachePath];
}

- (NSString *)libCachePath
{
	
	NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
	NSString * path = [[paths objectAtIndex:0] stringByAppendingFormat:@"/Caches"];
	
	[self touch:path];
	
	
	return path;
}

+ (NSString *)tmpPath
{
	return [[AppInfo sharedInstance] tmpPath];
}

- (NSString *)tmpPath
{
	
	NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
	NSString * path = [[paths objectAtIndex:0] stringByAppendingFormat:@"/tmp"];
	
	[self touch:path];
	
	return path;
}


+ (BOOL)touch:(NSString *)path
{
	return [[AppInfo sharedInstance] touch:path];
}

- (BOOL)touch:(NSString *)path
{
	if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:path] )
	{
		return [[NSFileManager defaultManager] createDirectoryAtPath:path
										 withIntermediateDirectories:YES
														  attributes:nil
															   error:NULL];
	}
	
	return YES;
}

+ (BOOL)touchFile:(NSString *)file
{
	return [[AppInfo sharedInstance] touchFile:file];
}

- (BOOL)touchFile:(NSString *)file
{
	if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:file] )
	{
		return [[NSFileManager defaultManager] createFileAtPath:file
													   contents:[NSData data]
													 attributes:nil];
	}
	
	return YES;
}


@end
